<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29 0029
 * Time: 下午 2:20
 */
namespace app\api\controller;
use app\admin\Model\Classify;
use app\admin\model\Shop as Shomodel;
use think\Db;

class Shop extends Baseapi{
    /**
     * @param $id $user_id
     * @return \think\response\Jsonp
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取商户的所有关联的信息
     */
    public function getdetial($id,$user_id=0){
        $info=Shomodel::getonerelation($id,$user_id);
        if($info['status']){
            foreach ($info['shopremark'] as $k=>$v){
                $info['shopremark'][$k]['s_createtime']=date('Y-m-d H:i',$v['s_createtime']);
            }
        }
        return jsonp($info);
    }

    /**
     * @return \think\response\Jsonp
     * @throws \think\exception\DbException
     * 商铺的分类列表
     */
    public function getshopclassify(){
        $list=Classify::all(function ($query){
                 $query->field('id,classify_name')
                       ->order('sort asc');
        });
        return jsonp($list);
    }

    /**
     * @return \think\response\Jsonp
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 店铺列表
     * 根据关键字搜索  $keywords  搜店铺的名称；
     * 根据分类搜索    $shopclassify  锅炉店铺的数量
     * 分页            $page  默认为第一页
     */
    public function getshoplists()
    {
        $shopclassify = input('shopclassify');
        $keywords = input('keywords');
        $page = input('page') ? input('page') : 1;
        if ($keywords) {
            $where['shop_name|shop_intro'] = ['like', '%' . $keywords . '%'];
        }
        $where['shop_status'] = 1;
        $list = Db::name('shop')->where($where)->page($page, 15)->field('id,shop_logo,shop_name,sale_num,classify_id')->select();
        if ($shopclassify) {
            foreach ($list as $k => $v) {
                $classifyarray = explode(',', $v['classify_id']);
                if (!in_array($shopclassify, $classifyarray)) {
                    unset($list[$k]);
                }
            }
        }
        $list = array_values($list);
        if ($page == 1 and !count($list)) {
            $data['status'] = 1;//状态1 为分页数为1 数据为空
        } else if ($page == 1 and count($list) > 0) {
            $data['status'] = 2;//状态2 为分页数为1 数据不为空
        } else if ($page > 1 and !count($list)) {
            $data['status'] = 3;//状态3 为分页数大于1 数据为空
        } else if ($page > 1 and count($list)) {
            $data['status'] = 4;//状态4 为分页数大于1 数据不为空
        }
        $data['list'] = $list;
        return jsonp($data);
    }
}